using Godot;
using System;
using System.Collections.Generic;

public partial class UpgradeScreen : CanvasLayer
{
	[Export]
	PackedScene upgradeCardScrene;

	[Signal] 
	public delegate void UpgradeSelectedEventHandler(AbilityUpgrade upgrade);
	
	private HBoxContainer cardContainer;


	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		cardContainer = GetNode<HBoxContainer>("%CardContainer");
		GetTree().Paused = true;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}

	//设置技能选择
	public void SetAbilityUpgrades(List<AbilityUpgrade> upgrades){
		float delay = 0;
		foreach(AbilityUpgrade upgrade in upgrades){
			AbilityUpgradeCard cardInstance = upgradeCardScrene.Instantiate() as AbilityUpgradeCard;
			cardContainer.AddChild(cardInstance);
			cardInstance.SetAbilityUpgrade(upgrade);
			cardInstance.PlayIn(delay);
			cardInstance.Selected += () => OnSelected(upgrade);
			delay += 0.2f;
		}
	}

	//所选取的技能
	public void OnSelected(AbilityUpgrade upgrade){
		this.EmitSignal(nameof(UpgradeSelected),upgrade);
		GetNode<AnimationPlayer>("AnimationPlayer").Play("out");
		GetNode<AnimationPlayer>("AnimationPlayer").AnimationFinished += (animName)=>{
			if(animName == "out"){
				GetTree().Paused = false;
				QueueFree();
			}
		};
		
	}
}
